package com.hongyuan.sellfitness.ui.home.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.data.md5.BaseUtil;
import com.hongyuan.sellfitness.databinding.ActivityBuyCardBinding;
import com.hongyuan.sellfitness.my_view.time_selecter.use_time.GetTimeData;
import com.hongyuan.sellfitness.ui.home.beans.SalerBean;
import com.hongyuan.sellfitness.ui.home.view_mode.BuyCardViewModel;
import com.hongyuan.sellfitness.utils.CustomDialog;
import com.hongyuan.sellfitness.utils.TimeUtil;

import java.util.List;

public class BuyCardActivity extends CustomActivity<ActivityBuyCardBinding, BuyCardViewModel> {

    //销售顾问
    private List<SalerBean.ListBean> mList;

    //生日选择器
    private GetTimeData birthDay;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_buy_card;
    }

    @Override
    public int initVariableId() {
        //注意了：这里是小写的viewModel
        return BR.viewModel;
    }

    @Override
    public BuyCardViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(BuyCardViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();

        setOnRefresh(binding.refresh,REFRESH_0X4);
        binding.comBack.setOnClickListener(view -> finish());

        //初始化生日选择器
        birthDay = new GetTimeData(this, (date, v) -> {
            String birth_date = TimeUtil.getStringByFormat(date,TimeUtil.dateFormatYMD);
            viewModel.info.get().setMi_birth(birth_date);
            viewModel.info.notifyChange();
        }, TimeUtil.getCurrentDateFormat(),TimeUtil.getCurrentDateFormat(),GetTimeData.YMD);

    }

    @Override
    public void initData() {
        super.initData();

        viewModel.clearParams().setParams("card_id",getBundle().getString("card_id"));

        viewModel.requestData(Constants.GET_CARD_INFO);
        viewModel.requestData(Constants.GET_M_INFO);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.setSalers.observe(this, s -> {
            mList = s;
        });
        viewModel.uc.showSaler.observe(this, s -> {
           if(mList != null && mList.size() > 0){
               CustomDialog.scroller(this, viewModel.rUtils.getUnitList(mList), "选择销售顾问", (v1, message,id) -> {
                   binding.selectAdviser.setText(message);
                   viewModel.salerName = id;
               });
           }else{
               ToastUtils.showShort("暂无顾问！");
           }
        });
        viewModel.uc.showCouponName.observe(this,s -> {
            if(BaseUtil.isValue(s)){
                binding.selectCoupon.setText(s);
            }else{
                binding.selectCoupon.setText("请选择");
            }

        });

        viewModel.uc.selectSex.observe(this, aVoid -> {
            CustomDialog.updateSex(this, viewModel.info.get().getMi_sex(), (v, message) -> {
                viewModel.info.get().setMi_sex(Integer.parseInt(message));
                viewModel.info.notifyChange();

            });
        });

        viewModel.uc.selectBirth.observe(this,aVoid -> {
            birthDay.showTime();
        });
    }
}
